package qmm.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet {
	public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String methodName = req.getParameter("method");

		if (methodName == null || methodName.trim().isEmpty()) {
			throw new RuntimeException("您没有传递 method 参数! 无法确定您想调用的方法");
		}
		Class c = this.getClass();
		Method method = null;
		try {
			method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
		} catch (Exception e) {
			throw new RuntimeException("您要调用的方法" + methodName + ",它不存在!");
		}
		try {
			method.invoke(this, req, resp);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
